package cn.itcast.tags.index.coprocessor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;
/**
 * 提交定时器
 */
public class CommitTimer extends TimerTask {
    private static final Logger logger = LoggerFactory.getLogger(CommitTimer.class);
    @Override
    public void run() {
        SolrWriter.instance.commitLock.lock();
        try {
            //cache中有数据则提交
            if (SolrWriter.cache.size() > 0) {
                logger.info("timer commit, count:" + SolrWriter.cache.size());
                SolrWriter.instance.inputDoc(SolrWriter.cache);
                SolrWriter.instance.cache.clear(); // 清空缓存
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            SolrWriter.instance.commitLock.unlock();
        }
    }
}
